﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Atendex.Web
{
    public partial class CalendarCombo : System.Web.UI.UserControl
    {
        private readonly IEnumerable<ListItem> _meses;
        private int _intervaloAno = 10;

        private static readonly IEnumerable<ListItem> MesesPortugues = new List<ListItem>(12)
                         {
                             new ListItem("Jan", "1"),
                             new ListItem("Fev", "2"),
                             new ListItem("Mar", "3"),
                             new ListItem("Abr", "4"),
                             new ListItem("Mai", "5"),
                             new ListItem("Jun", "6"),
                             new ListItem("Jul", "7"),
                             new ListItem("Ago", "8"),
                             new ListItem("Set", "9"),
                             new ListItem("Out", "10"),
                             new ListItem("Nov", "11"),
                             new ListItem("Dez", "12")
                         };

        private DateTime _dataInicial = DateTime.Now;

        public CalendarCombo()
            : this("Data", MesesPortugues)
        {
        }

        public CalendarCombo(string label)
            : this(label, MesesPortugues)
        {
        }

        public CalendarCombo(string label, IEnumerable<ListItem> meses)
        {
            Label = label;
            _meses = meses;
        }

        public string Label { get; set; }

        public DateTime DataInicial
        {
            get { return _dataInicial; }
            set { _dataInicial = value; }
        }

        public int IntervaloAno
        {
            get { return _intervaloAno; }
            set
            {
                if (value > 0) _intervaloAno = value;
            }
        }

        public DateTime DataEscolhida
        {
            get
            {
                int dia = int.Parse(ddlDia.SelectedValue);
                int mes = int.Parse(ddlMes.SelectedValue);
                int ano = int.Parse(ddlAno.SelectedValue);
                return new DateTime(ano, mes, dia);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CarregarCombos();
                ConfiguraCombosData(DataInicial);
            }
            else
            {
                ConfiguraCombosData(DataEscolhida);
            }
            Label1.Text = Label;
        }

        private void CarregarCombos()
        {
            for (int dia = 1; dia <= 31; dia++)
                ddlDia.Items.Add(dia.ToString());

            foreach (ListItem mes in _meses)
                ddlMes.Items.Add(mes);

            int anoInicial = DataInicial.Year - IntervaloAno;
            int anoFinal = DataInicial.Year + IntervaloAno;
            for (int ano = anoInicial; ano < anoFinal; ano++)
                ddlAno.Items.Add(ano.ToString());
        }

        private void ConfiguraCombosData(DateTime data)
        {
            SelecionaOpcaoCombo(ddlDia, data.Day.ToString());
            SelecionaOpcaoCombo(ddlMes, data.Month.ToString());
            SelecionaOpcaoCombo(ddlAno, data.Year.ToString());
        }

        private static void SelecionaOpcaoCombo(ListControl combo, string value)
        {
            foreach (ListItem item in combo.Items)
            {
                if (item.Value == value)
                {
                    combo.ClearSelection();
                    item.Selected = true;
                    break;
                }
            }
        }
    }
}